﻿using Microsoft.AspNetCore.Mvc;
using MyWeb.Common.Helper;
using MyWeb.Model.User;
using MyWeb.Model;
using MyWeb.Repository.UnitOfWorks;
using SqlSugar;
using Microsoft.AspNetCore.Authorization;
using Newtonsoft.Json;
using MyWeb.Extensions.ServiceExtensions.Swagger;
using static MyWeb.Extensions.ServiceExtensions.Swagger.CustomApiVersion;
using Microsoft.AspNetCore.Mvc.RazorPages;
using MyWeb.Business.System;
using MyWeb.Business.System.Impl;
using MyWeb.Model.Models;
using MyWeb.Model.Dto;

namespace MyWeb.Api.Controllers.V1.System
{

    /// <summary>
    /// 菜单管理
    /// </summary>
    [Produces("application/json")]
    [CustomRoute(ApiVersions.V1)]
    public class PermissionController : BaseController
    {
        private readonly SysUserInfo _user;
        private readonly IPermissionService _permissionService;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="user"></param>
        /// <param name="permissionService"></param>
        public PermissionController(SysUserInfo user, IPermissionService permissionService)
        {
            _permissionService = permissionService;
            _user = user;
        }

        /// <summary>
        /// 获取菜单列表
        /// </summary>
        /// <param name="page">页码</param>
        /// <param name="key">搜索关键字</param>
        /// <param name="pageSize">页面大小</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult> Get(int page = 1, string key = "", int limit = 50)
        {
            if (string.IsNullOrEmpty(key) || string.IsNullOrWhiteSpace(key))
            {
                key = "";
            }
            var pageModel = await _permissionService.Get(page,key, limit);

            ////获取用户信息
            //var user = Request.Headers["UserInfo"].FirstOrDefault();
            //Console.WriteLine(user);

            return Success(pageModel);

        }

        /// <summary>
        /// 获取菜单表格数
        /// </summary>
        /// <param name="pid"></param>
        /// <param name="key"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult> GetTreeTable(long pid = 0, string key = "", int pageSize = 50)
        {
            if (string.IsNullOrEmpty(key) || string.IsNullOrWhiteSpace(key))
            {
                key = "";
            }
            var data = await _permissionService.GetTreeTable(pid,key);

            return Success(data);
        }

        /// <summary>
        /// 获取所有菜单数
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult> GetPermissionTree(long pid = 0)
        {
            var rootRoot = await _permissionService.GetPermissionTree(pid);
            return Success(rootRoot);
        }

        /// <summary>
        /// 获取用户菜单
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult> GetNavigationBar(long uid = 12)
        {
            var rootRoot = await _permissionService.GetNavigationBar(uid);
            return Success(rootRoot);

        }

        /// <summary>
        /// 获取角色菜单
        /// </summary>
        /// <param name="rid">角色Id</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult> GetRoleNavigationBar(long rid = 25)
        {
            var ids = await _permissionService.GetRoleNavigationBar(rid);

            return Success(ids);
        }

        /// <summary>
        /// 保存菜单权限分配
        /// </summary>
        /// <param name="assign"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ApiResult> PutPermission([FromBody] AssignView assign)
        {

            if (assign.rid <= 0)
            {
                return Failed<object>("请正确选择角色");
            }
            var num = await _permissionService.PutPermission(assign);
            return Success<object>(num);
        }

        /// <summary>
        /// 删除菜单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<ApiResult> DelPermission(long id)
        {

            if (id <= 0)
            {
                return Failed<object>("请正确选择菜单");
            }
            var num = await _permissionService.DelPermission(id);
            return Success<object>(num);
        }
        /// <summary>
        /// 保存菜单
        /// </summary>
        /// <param name="permission"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> SavaPer([FromBody]Permission permission)
        {
            var user = Request.Headers["UserInfo"].FirstOrDefault();
            SysUserInfo? userInfo = JsonConvert.DeserializeObject<SysUserInfo>(user);
            if(permission.Code.Length <= 0)
            {
                permission.Code = " ";
            }
            if (permission.Id > 0)
            {
                //修改
                return Success<object>(await _permissionService.UpdatePermission(permission,userInfo));
            }
            else
            {
                //新增
                return Success<object>(await _permissionService.InsertPermission(permission,userInfo));
            }
        }
        /// <summary>
        /// 获取菜单列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult> GetPermissionList()
        {
            return Success<object>(await _permissionService.GetPermissionList());
        }
    }
}
